package com.yootk.rabbitmq.consumer.direct;

import com.rabbitmq.client.*;

import java.io.IOException;

public class MessageConsumerCGroup {
    private static final String ROUTING = "muyan.routingkey.direct" ;
    private static final String QUEU_NAME = "yootk.c.queu.msg" ;
    private static final String HOST = "rabbitmq-server" ;
    private static final int PORT = 5672 ;
    private static final String USERNAME = "yootk" ;
    private static final String PASSWORD = "hello" ;
    private static final String VHOST = "MuyanVHost" ;
    private static final String EXCHANGE_NAME ="yootk.exchange.direct" ;
    private static int count = 0 ;

    public static void main(String[] args) throws Exception{
        ConnectionFactory factory = new ConnectionFactory() ;
        factory.setHost(HOST);
        factory.setPort(PORT);
        factory.setUsername(USERNAME);
        factory.setPassword(PASSWORD);
        factory.setVirtualHost(VHOST);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        Long start = System.currentTimeMillis() ;
        channel.exchangeDeclare(EXCHANGE_NAME,"direct") ;
        channel.queueDeclare(QUEU_NAME,true,false,true,null) ;
        channel.queueBind(QUEU_NAME,EXCHANGE_NAME,ROUTING) ;
        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body) ;
                System.out.println("【消费组C】接收消息：" + message);
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        } ;
        channel.basicConsume(QUEU_NAME,consumer) ;
    }
}
